<?php
/**
 * Author: liqinglin
 * CreateTime: 2019-1-31 11:28
 * description:
 */

namespace app\admin\controller;

use app\admin\model\ServiceErrorMessage;
use think\Db;
use Im\Yunxinapi;
use think\Request;

class Servicetypes extends Base
{

    //服务商类型列表
    public function Typelist()
    {
        $data = Db::table('cn_service_types')->where('is_del', 0)->paginate();
        return view('/service/service_type_list', compact('data'));
    }

    //增加服务商类别
    public function add()
    {
        if (Request::instance()->isGet()) {
            $service_type_id = input('service_type_id');
            if ($service_type_id) {
                $data = Db::table('cn_service_types')->where('service_type_id', $service_type_id)->find();
                return view('/service/typeedit', compact('data'));
            } else {
                return view('/service/typeadd');
            }
        } elseif (Request::instance()->isPost()) {
            $service_type_name = input('service_type_name');
            $service_type_name_en = input('service_type_name_en');
            $service_type_id = input('service_type_id');
            if ($service_type_name && $service_type_name_en) {
                if ($service_type_id) {
                    DB::table('cn_service_types')->where('service_type_id', $service_type_id)
                        ->update([
                            'service_type_name' => $service_type_name,
                            'service_type_name_en' => $service_type_name_en
                        ]);
                }else{
                    DB::table('cn_service_types')->insert([
                        'service_type_name' => $service_type_name,
                        'service_type_name_en' => $service_type_name_en
                    ]);
                }
                return json(['status' => 200, 'msg' => lang('成功')]);
            } else {
                return json(['status' => 201, 'msg' => lang('不能为空')]);
            }
        }

    }

    public function del()
    {
        $service_type_id = input('service_type_id');
        if ($service_type_id) {
            DB::table('cn_service_types')->where('service_type_id', $service_type_id)->update(['is_del' => 1]);
            return json(['status' => 200, 'msg' => lang('成功')]);
        } else {
            return json(['status' => 201, 'msg' => lang('不能为空')]);
        }

    }
}